<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form label-width="70px" class="login_form" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="用户名:" prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.username"
            @keyup.native.13="login('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            prefix-icon="el-icon-bicycle"
            v-model="form.password"
            type="password"
            @keyup.native.13="login('ruleForm')"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login('ruleForm')">登录</el-button>
          <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    // 绑定键盘监听事件
    console.log(this.$route, this.$router)
  },
  methods: {
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    login (formName) {
      // 使用elementui 的validate函数
      //  回调函数第一个参数为校验是否成功的布尔值  第二个(返回的是对象,对象中的属性为未校验成功的属性)
      this.$refs[formName].validate(async (valid, prop) => {
        if (valid) {
          // 使用axios发送请求
          const res = await this.axios.post('/login', this.form)
          console.log(res)
          console.log(this.$message.close)

          if (res.meta.status !== 200) {
            // 防止反复提示登录失败
            this.$message.close()
            this.$message.error({
              message: '登陆失败'
            })
          } else {
            sessionStorage.setItem('token', res.data.token)
            this.$message({
              message: '登陆成功',
              type: 'success'
            })
            this.$router.push('/home')
          }
        }
      })
    }
  }
}
</script>

<style  lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    width: 100%;
    position: absolute;
    bottom: 0;
    box-sizing: border-box;
    padding: 0 20px;
  }
}
</style>
